package com.hbzhit.hander;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.hbzhit.common.utils.Constant;
import com.hbzhit.common.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * MyBatisPlus 自动填充Hander
 * @author DING
 */
@Component
@Slf4j
public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        try {
            this.fillStrategy(metaObject, "creator", ShiroUtils.getUserId());
            this.fillStrategy(metaObject, "orgId", ShiroUtils.getUser().getOrgId());
        } catch (Exception e) {
            log.debug("自动填充creator & orgId失败");
        }
        this.fillStrategy(metaObject, "isDel", Constant.INT_N);
        this.fillStrategy(metaObject, "createTime", new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            this.fillStrategy(metaObject, "modifier", ShiroUtils.getUserId());
        } catch (Exception e) {
            log.debug("自动填充modifier失败");
        }
        this.fillStrategy(metaObject, "modifyTime", new Date());
    }
}
